<?php

class SessionSaveHandlerFile implements SessionSaveHandler {
	static $maxLifeTime = 1440;
	static $savePath = '/tmp';
	private $handler = NULL;
	function __construct($settings, $options) {
		is_array ( $options ) && isset ( $options ['gc_maxlifetime'] ) && self::$maxLifeTime = $options ['gc_maxlifetime'];
		if (isset ( $settings ['file'] ['savepath'] )) {
			self::$savePath = $settings ['file'] ['savepath'];
		} else {
			if (is_array ( $options ) && isset ( $options ['save_path'] )) {
				self::$savePath = $options ['save_path'];
			} else {
				self::$savePath = ini_get ( 'session.save_path' );
			}
		}
		if (empty ( self::$savePath )) {
			trigger_error ( 'File Session Init failed save path empty.', E_USER_ERROR );
			return false;
		}
	}
	
	function open($save_path, $session_name) {
		//self::$savePath = $save_path;
		return true;
	}
	
	function close() {
		return true;
	}
	
	function read($session_id) {
		$sess_file = self::$savePath . "/sess_{$session_id}";
		return is_file($sess_file)?@file_get_contents( $sess_file ):'';
	}
	
	function write($session_id, $session_data) {
		$sess_file = self::$savePath . "/sess_{$session_id}";
		if ($fp = @fopen ( $sess_file, "w" )) {
			$return = fwrite ( $fp, $session_data );
			fclose ( $fp );
			return $return;
		} else {
			return (false);
		}
	}
	
	function destroy($session_id) {
		$sess_file = self::$savePath . "/sess_{$session_id}";
		return @unlink ( $sess_file );
	}
	
	function gc($maxlifetime = 0) {
		foreach ( glob ( self::$savePath . "/sess_*" ) as $filename ) {
			if (filemtime ( $filename ) + self::$maxLifeTime < time ()) {
				@unlink ( $filename );
			}
		}
		return true;
	}
}
?>